package cn.hy.spider.x6o;

import cn.hutool.core.collection.ListUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONArray;
import cn.hutool.json.JSONException;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.hy.spider.HeaderUtil;
import cn.hy.spider.HttpClientUtil;
import cn.hy.spider.Picture;
import cn.hy.spider.PictureSpider;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

public class App {

    private final static List<String> exclude = ListUtil.toList(
            // "JVID",
            //"合集",
            "王动作品",
            "Twitter萝莉", "蠢沫沫",
            "鱼子酱Fish - 电竞女孩", "鱼子酱Fish – 游戏空间", "鱼子酱Fish - 用爱发电", "过期米线线喵 – 初恋", "钛合金TiTi - 天竺少女"
            , "希威社 北舞美女彤彤", "希威社 Yara 依然", "校花也是大姐头", "可爱女仆"


    );
    // 备选 奈汐酱 ,摄影师 浩廷,Wuyo , ,"爆机少女喵小吉",jbswg_diary
    private final static List<String> include = ListUtil.toList(
//            "过期米线线喵",
//            "鱼子酱", "二阶堂",
//            "女主K",
//            "峰不二子","钛合金TiTi",
//            "Arty Huang", "小恩" ,"atfmaker", "GlamARchive","阳布布鸭","Bimilstory" ,"布丁大法",
//            "希威社", "阿雪雪","jiaduobao" ,"阳布布鸭","布丁大法","GlamARchive"
//            "Meriol" ,"橙子喵酱","小青茗"
//            "Yeon Woo",
//            "希威社 摄影师未流出图片精选合集", "飘飘" ,"阳布布鸭 合集",
            "媚肉夜勤病房","一团墨染","猫屋少女", "华丽婚纱淫靡新娘-洞房花烛夜", "爆机少女喵小吉"
    );

    public static void main(String[] args) throws IOException, InterruptedException {

        PictureSpider pictureSpider = new PictureSpider();

        String basePath = "/Users/life/pic/x6o";
        String urlTemplate = "https://x6o.com/api/topics/14/articles?page={}&per_page=80&order=-create_time&include=user%2Ctopics%2Cis_following";

        CloseableHttpClient httpClient = HttpClientUtil.getHttpClient();
        RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(14999).setConnectTimeout(15000)
                .setConnectionRequestTimeout(14999).setProxy(new HttpHost("127.0.0.1", 7890)).build();

        Random random = new Random();
        for (int i = 1; i <= 120; i++) {
            String url = StrUtil.format(urlTemplate, i);
            HttpGet get = new HttpGet(url);
            get.setConfig(requestConfig);
            get.setHeader("User-Agent", HeaderUtil.headers[random.nextInt(HeaderUtil.headers.length)]);
            CloseableHttpResponse response = httpClient.execute(get);
            HttpEntity entity = response.getEntity();
            String entityStr = EntityUtils.toString(entity);
            JSONObject jo = null;
            try {
                jo = JSONUtil.parseObj(entityStr);
            } catch (JSONException e) {
                // ignore
                continue;
            }
            JSONArray array = jo.getJSONArray("data");
            for (Object obj : array) {
                Set<Picture> set = new HashSet<>();
                JSONObject d = (JSONObject) obj;
                String title = d.getStr("title");
//                System.out.println("title = " + title);
                if (StrUtil.isNotBlank(title) && isInExclude(title)) {
                    continue;
                }
                if (StrUtil.isNotBlank(title) && isInInclude(title)) {
                    String savePath = basePath + File.separator + title;
                    if (!FileUtil.exist(savePath)) {
                        FileUtil.mkdir(savePath);
                    }
                    String contentMarkdown = d.getStr("content_markdown");
//                    System.out.println("contentMarkdown = " + contentMarkdown);
//                    List<String> all = ReUtil.findAll("(https:.*)", contentMarkdown, 0);
                    List<String> all = ReUtil.findAll("https?://[^\\s\"]+", contentMarkdown, 0);
                    for (String group : all) {
                        Picture picture = new Picture();
                        String name = FileUtil.getName(group);
                        picture.setTitle(name);
                        picture.setUrl(group);
                        picture.setFilePath(savePath);
                        set.add(picture);
                    }
                    System.out.printf("page: %d, size: %d, title: %s \n\t", i, set.size(), title);
                    pictureSpider.start(set);
//                    List<List<Picture>> split = CollUtil.split(set, 30);
//                    for (List<Picture> pictures : split) {
//                    }
                }
            }
        }

        pictureSpider.stop();
    }


    public static void addExclude(String title) {
        exclude.add(title);
    }

    public static boolean isInExclude(String title) {
        Assert.notBlank(title);
        for (String match : exclude) {
            if (title.contains(match)) {
                return true;
            }
        }
        return false;
    }


    public static void addInclude(String title) {
        include.add(title);
    }

    public static boolean isInInclude(String title) {
        Assert.notBlank(title);
        for (String match : include) {
            if (title.contains(match)) {
                return true;
            }
        }
        return false;
    }
}
